{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a12cff04-dcca-4bc9-a360-0d60916f67d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "756c029c-db22-474c-9ff4-a337c3a263fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "def PCA(X, d):\n",
    "    #X(m, n),n个m维列向量\n",
    "    #中心化\n",
    "    X -= np.sum(X, axis=1).reshape((X.shape[0], 1))\n",
    "    \n",
    "    #对XX^T做特征值分解\n",
    "    lamda, W = np.linalg.eig(X.dot(X.T))\n",
    "    \n",
    "    #排序\n",
    "    idx = np.argsort(lamda)\n",
    "    return W[:, idx[-d:]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67bdd005-e5c6-46b6-a3bd-a657d94f25fd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
